<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="initdata()" size="small">查询搜索</el-button>
        <el-button style="float:right;margin-right: 15px" @click="resetfiled()" size="small">重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="分类名称">
            <el-input v-model="listQuery.name" class="input-width" placeholder="请输入分类名称" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="isadd()" style="margin-left: 20px">添加</el-button>
    </el-card>

    <div style="margin:0px;" v-loading="loading">
      <el-table
        :data="tableData"
        border
        stripe
        row-key="id"
        :indent="32"
        :tree-props="{children: 'subs', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="name" label="分类名称" align="left"></el-table-column>
        <el-table-column prop="parentname" label="上级分类" align="center">
          <!-- <template slot-scope="scope">{{scope.row.parentId | getparentname}}</template> -->
        </el-table-column>
        <el-table-column prop="comment" label="备注" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <!-- <el-button type="primary" @click="delrecord(scope.row)">附加属性</el-button> -->
            <el-button type="primary" @click="delrecord(scope.row)">删除</el-button>
            <el-button type="primary" @click="showupdaterecord(scope)">更新</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="text-align:center;">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
        background
      ></el-pagination>
    </div>
    <el-dialog
      :title="isupdate ? '更新' : '添加'"
      :visible.sync="isadddia"
      center
      width="40%"
      @close="isadddia = false"
    >
      <el-form
        :model="refform"
        ref="refform"
        label-width="120px"
        style="margin: 0px 70px;"
        :rules="rules"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="refform.name"></el-input>
        </el-form-item>
        <el-form-item label="上级分类" prop>
          <!-- <el-input v-model="refform.parentId"></el-input> -->
          <treeselect
            v-model="selecttreelabel"
            :options="selectMenuList"
            :normalizer="normalizer"
            placeholder="选择上级类目"
            :clearable="false"
            @select="treeselectclick"
          ></treeselect>
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input v-model="refform.comment"></el-input>
        </el-form-item>

        <el-table :data="refform.attrs" border stripe>
          <el-table-column prop="customTag" label="标签KEY" width>
            <template slot-scope="scope">
              <el-input v-model="scope.row.customTag" label></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="标签名" width>
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" label></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="defaultValue" label="默认值" width>
            <template slot-scope="scope">
              <el-input v-model="scope.row.defaultValue" label></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="isRequire" label="是否为必填" width>
            <template slot-scope="scope">
              <el-radio-group v-model="scope.row.isRequire">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column prop="comment" label="标签备注" width>
            <template slot-scope="scope">
              <el-input v-model="scope.row.comment" label></el-input>
            </template>
          </el-table-column>
          <el-table-column prop label="操作" width>
            <template slot-scope="scope">
              <!-- {{scope}} -->
              <el-button type="primary" @click="delattr(scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- <div style="text-align: center;">
          <el-button type="primary" @click="addattr()" style="margin-bottom: 10px;">添加附加属性</el-button>
          <el-button type="primary" @click="delattr()" style="margin-bottom: 10px;">删除附加属性</el-button>
        </div>
        <el-divider></el-divider>-->
      </el-form>
      <span slot="footer">
        <el-button @click="addattr">添加附加属性</el-button>
        <el-button @click="isadddia = false">取 消</el-button>
        <el-button type="primary" @click="isupdate ? updaterecord() : addrecord()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import devTypeService from "@/api/devTypeService";
// import the component
import Treeselect from "@riophae/vue-treeselect";
// import the styles
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  components: { Treeselect },
  data() {
    return {
      loading: true,
      listQuery: {
        name: "",
        parentId: "",
        comment: ""
      },
      pageSize: 5,
      currentPage: 1,
      totalNum: 0,
      isupdate: false,
      isshowqr: false,
      qrdata: "",
      tableData: [],
      isadddia: false,
      refform: {
        name: "",
        parentId: 0,
        comment: "",
        attrs: []
      },
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        comment: [{ required: true, message: "不能为空", trigger: "blur" }]
      },
      selectMenuList: [],
      selecttreelabel: 0
    };
  },
  computed: {},
  watch: {},
  mounted() {
    this.initdata();
  },
  filters: {
    getparentname(val) {
      let name = "";

      this.selectMenuList.map(item => {
        if (item.id == val) {
          name = item.name;
        }
      });

      return name;
    }
  },
  methods: {
    showupdaterecord(scope) {
      this.isupdate = true;
      this.refform = _.cloneDeep(scope.row);
      this.selecttreelabel = this.refform.parentId;
      this.isadddia = true;
    },
    treeselectclick(node) {
      this.refform.parentId = node.id;
    },
    normalizer(node) {
      if (node.subs) {
        if (node.subs.length == 0) {
          return {
            id: node.id,
            label: node.name
          };
        } else {
          return {
            id: node.id,
            label: node.name,
            children: node.subs
          };
        }
      } else {
        return {
          id: node.id,
          label: node.name
        };
      }
    },
    addattr() {
      let attr = {
        customTag: "",
        name: "",
        defaultValue: "",
        isRequire: 0,
        comment: ""
      };
      this.refform.attrs.push(attr);
    },
    delattr(scope) {
      this.refform.attrs.splice(scope.$index, 1);
    },
    sizeChange(val) {
      this.pageSize = val;
      this.initdata();
    },
    currentChange(val) {
      this.currentPage = val;
      this.initdata();
    },

    delrecord(row) {
      devTypeService.del({ ids: row.id }).then(res => {
        this.initdata();
        this.$message.success("删除成功");
      });
    },
    updaterecord() {
      this.$refs.refform.validate(valid => {
        if (valid) {
          devTypeService.update(this.refform).then(res => {
            this.isadddia = false;
            this.initdata();
            this.$message.success("修改成功");
          });
        }
      });
    },
    initdata() {
      this.loading = true;
      devTypeService
        .select({
          page: 1,
          count: 1000,
          isTree: 1
        })
        .then(res => {
          this.selectMenuList = res.data.records;
          this.selectMenuList.unshift({ id: 0, name: "无上级分类" });
          this.refform.parentId = 0;
          this.selecttreelabel = 0;
          devTypeService
            .select({
              page: this.currentPage,
              count: this.pageSize,
              name: this.listQuery.name,
              isAttr: 1,
              isTree: 1
            })
            .then(res => {
              this.getrealparentname(res.data.records);
              this.tableData = res.data.records;
              this.totalNum = res.data.total;
              // this.$message.success("数据加载成功");
              this.loading = false;
            });
        });
    },

    getrealparentname(itemarr) {
      itemarr.map(item => {
        item.parentname = "";
        this.selectMenuList.map(i => {
          if (i.id == item.parentId) {
            item.parentname = i.name;
          }
        });
        if (item.subs != null && item.subs.length != 0) {
          this.getrealparentname(item.subs);
        }
      });
    },

    isadd() {
      this.isadddia = true;
      this.isupdate = false;
      this.resetinputdia();
    },

    resetinputdia() {
      this.refform = {
        name: "",
        parentId: 0,
        comment: "",
        attrs: []
      };
    },

    resetfiled() {
      this.listQuery.name = "";
      this.listQuery.parentId = "";
      this.listQuery.comment = "";
    },

    addrecord() {
      // this.isadddia = true
      this.$refs.refform.validate(valid => {
        if (valid) {
          devTypeService.add(this.refform).then(res => {
            this.isadddia = false;
            this.initdata();
            this.$message.success("添加成功");
          });
        }
      });
    }
  }
};
</script>
<style lang='scss' scoped>
</style>